From a601d049feff5768b0d99b9fcec807b277fa4c81 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 29 Aug 2014 12:00:15 -0700 Subject: [PATCH] Be compatible with the stdlib for now ('static shell) --- src/cargo/core/shell.rs | 48 +++++++++++++++---------------- src/cargo/lib.rs | 2 +- src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/util/config.rs | 2 +- tests/test_cargo_cross_compile.rs | 4 +-- tests/test_shell.rs | 37 +++++++++++++----------- 6 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index 9c741e1ab..4778929a1 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -10,34 +10,34 @@ pub struct ShellConfig { pub tty: bool } -enum AdequateTerminal<'a> { - NoColor(Box), - Color(Box>+'a>) +enum AdequateTerminal { + NoColor(Box), + Color(Box>+'static>) } -pub struct Shell<'a> { - terminal: AdequateTerminal<'a>, +pub struct Shell { + terminal: AdequateTerminal, config: ShellConfig } -pub struct MultiShell<'a> { - out: Shell<'a>, - err: Shell<'a>, +pub struct MultiShell { + out: Shell, + err: Shell, verbose: bool } pub type Callback<'a> = |&mut MultiShell|:'a -> IoResult<()>; -impl<'a> MultiShell<'a> { - pub fn new(out: Shell<'a>, err: Shell<'a>, verbose: bool) -> MultiShell<'a> { +impl MultiShell { + pub fn new(out: Shell, err: Shell, verbose: bool) -> MultiShell { MultiShell { out: out, err: err, verbose: verbose } } - pub fn out(&mut self) -> &mut Shell<'a> { + pub fn out(&mut self) -> &mut Shell { &mut self.out } - pub fn err(&mut self) -> &mut Shell<'a> { + pub fn err(&mut self) -> &mut Shell { &mut self.err } @@ -72,17 +72,17 @@ impl<'a> MultiShell<'a> { } } -pub type ShellCallback<'a> = |&mut Shell<'a>|:'a -> IoResult<()>; +pub type ShellCallback<'a> = |&mut Shell|:'a -> IoResult<()>; -impl<'a> Shell<'a> { - pub fn create(out: Box, config: ShellConfig) -> Shell<'a> { +impl Shell { + pub fn create(out: Box, config: ShellConfig) -> Shell { if config.tty && config.color { - let term: Option>> = Terminal::new(out); + let term: Option>> = Terminal::new(out); term.map(|t| Shell { - terminal: Color(box t as Box>>), + terminal: Color(box t as Box>>), config: config }).unwrap_or_else(|| { - Shell { terminal: NoColor(box stderr() as Box), config: config } + Shell { terminal: NoColor(box stderr() as Box), config: config } }) } else { Shell { terminal: NoColor(out), config: config } @@ -121,8 +121,8 @@ impl<'a> Shell<'a> { } } -impl<'a> Terminal> for Shell<'a> { - fn new(out: Box) -> Option> { +impl Terminal> for Shell { + fn new(out: Box) -> Option { Some(Shell { terminal: NoColor(out), config: ShellConfig { @@ -168,18 +168,18 @@ impl<'a> Terminal> for Shell<'a> { } } - fn unwrap(self) -> Box { + fn unwrap(self) -> Box { fail!("Can't unwrap a Shell"); } - fn get_ref<'b>(&'b self) -> &'b Box { + fn get_ref<'b>(&'b self) -> &'b Box { match self.terminal { Color(ref c) => c.get_ref(), NoColor(ref w) => w } } - fn get_mut<'b>(&'b mut self) -> &'b mut Box { + fn get_mut<'b>(&'b mut self) -> &'b mut Box { match self.terminal { Color(ref mut c) => c.get_mut(), NoColor(ref mut w) => w @@ -187,7 +187,7 @@ impl<'a> Terminal> for Shell<'a> { } } -impl<'a> Writer for Shell<'a> { +impl Writer for Shell { fn write(&mut self, buf: &[u8]) -> IoResult<()> { match self.terminal { Color(ref mut c) => c.write(buf), diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index f01277bb4..8144eb396 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -150,7 +150,7 @@ pub fn process_executed<'a, } } -pub fn shell(verbose: bool) -> MultiShell<'static> { +pub fn shell(verbose: bool) -> MultiShell { let tty = stderr_raw().isatty(); let stderr = box stderr() as Box; diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index 23b0cf12f..8b686af2d 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -36,7 +36,7 @@ use util::{CargoResult, Wrap, config, internal, human, ChainError, profile}; pub struct CompileOptions<'a> { pub update: bool, pub env: &'a str, - pub shell: &'a mut MultiShell<'a>, + pub shell: &'a mut MultiShell, pub jobs: Option, pub target: Option<&'a str>, pub dev_deps: bool, diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 01e544383..6e03115ac 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -9,7 +9,7 @@ use util::toml as cargo_toml; pub struct Config<'a> { home_path: Path, - shell: &'a mut MultiShell<'a>, + shell: &'a mut MultiShell, jobs: uint, target: Option, linker: Option, diff --git a/tests/test_cargo_cross_compile.rs b/tests/test_cargo_cross_compile.rs index 7e65190d1..35b702a82 100644 --- a/tests/test_cargo_cross_compile.rs +++ b/tests/test_cargo_cross_compile.rs @@ -163,7 +163,7 @@ test!(plugin_deps { } fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) - -> Box { + -> Box { MacExpr::new(quote_expr!(cx, 1i)) } "#); @@ -245,7 +245,7 @@ test!(plugin_to_the_max { } fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) - -> Box { + -> Box { MacExpr::new(quote_expr!(cx, baz::baz())) } "#); diff --git a/tests/test_shell.rs b/tests/test_shell.rs index d1211ab2b..f0da10669 100644 --- a/tests/test_shell.rs +++ b/tests/test_shell.rs @@ -1,50 +1,55 @@ -use support::{ResultTest,Tap,shell_writes}; +use std::io::{MemWriter, IoResult, ChanReader, ChanWriter}; + +use term::{Terminal, TerminfoTerminal, color}; use hamcrest::{assert_that}; -use std::io::{MemWriter, BufWriter, IoResult}; + +use support::{ResultTest, Tap, shell_writes}; use cargo::core::shell::{Shell,ShellConfig}; -use term::{Terminal,TerminfoTerminal,color}; fn setup() { } -fn writer(buf: &mut [u8]) -> Box { - box BufWriter::new(buf) as Box +fn io_channel() -> (Box, Box) { + let (tx, rx) = channel(); + (box ChanWriter::new(tx), box ChanReader::new(rx)) } test!(non_tty { let config = ShellConfig { color: true, verbose: true, tty: false }; - let mut buf: Vec = Vec::from_elem(9, 0 as u8); + let (tx, mut rx) = io_channel(); - Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| { + Shell::create(tx, config).tap(|shell| { shell.say("Hey Alex", color::RED).assert(); - assert_that(buf.as_slice(), shell_writes("Hey Alex\n")); }); + assert_that(rx.read_to_end().unwrap().as_slice(), + shell_writes("Hey Alex\n")); }) test!(color_explicitly_disabled { let config = ShellConfig { color: false, verbose: true, tty: true }; - let mut buf: Vec = Vec::from_elem(9, 0 as u8); + let (tx, mut rx) = io_channel(); - Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| { + Shell::create(tx, config).tap(|shell| { shell.say("Hey Alex", color::RED).assert(); - assert_that(buf.as_slice(), shell_writes("Hey Alex\n")); }); + assert_that(rx.read_to_end().unwrap().as_slice(), + shell_writes("Hey Alex\n")); }) test!(colored_shell { let term: Option> = Terminal::new(MemWriter::new()); if term.is_none() { return } + let (tx, mut rx) = io_channel(); let config = ShellConfig { color: true, verbose: true, tty: true }; - let mut buf: Vec = Vec::from_elem(100, 0 as u8); - Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| { + Shell::create(tx, config).tap(|shell| { shell.say("Hey Alex", color::RED).assert(); - let buf = buf.as_slice().slice_to(buf.iter().position(|a| *a == 0).unwrap()); - assert_that(buf, shell_writes(colored_output("Hey Alex\n", - color::RED).assert())); }); + assert_that(rx.read_to_end().unwrap().as_slice(), + shell_writes(colored_output("Hey Alex\n", + color::RED).assert())); }) fn colored_output(string: S, color: color::Color) -> IoResult { -- 2.30.2